package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ResourceMapper;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;
    @Override
    public List<ResourceVo> findResourceList(ResourceDto resourceDto) {
        List<Resource> resourceList = resourceMapper.selectList(resourceDto);
        return BeanUtil.copyToList(resourceList, ResourceVo.class);
    }

    @Override
    public TreeVo resourceTreeVo(ResourceDto resourceDto) {
        ResourceDto dto = ResourceDto.builder()
                .dataState(SuperConstant.DATA_STATE_0)
                .parentResourceNo(NoProcessing.processString(SuperConstant.ROOT_PARENT_ID))
                .resourceType(SuperConstant.MENU)
                .build();


        List<Resource> resourceList = resourceMapper.selectList(dto);
        if(EmptyUtil.isNullOrEmpty(resourceList)){
            throw new RuntimeException("资源信息未定义");
        }

        Resource rootResource = new Resource();
        rootResource.setResourceNo(SuperConstant.ROOT_PARENT_ID);
        rootResource.setResourceName("智慧养老");
        List<TreeItemVo> treeItemVoList = new ArrayList<>();

        recursionTreeItem(treeItemVoList,rootResource, resourceList);



        return TreeVo.builder().items(treeItemVoList).build();
    }

    @Override
    public void createResource(ResourceDto resourceDto) {
        Resource resource = BeanUtil.toBean(resourceDto, Resource.class);
        //获取父级资源
        Resource parentResource = resourceMapper.selectByResourceNo(resource.getParentResourceNo());
        //获取父级资源状态,使得子资源与父级资源一致
        resource.setDataState(parentResource.getDataState());

        //判断节点是否为菜单
        Boolean isIgnore=true;
        if(resource.getResourceType().equals(SuperConstant.BUTTON)){
            isIgnore=false;
        }

        //根据父资源编号生成子资源编号
        String resourceNo=createResourceNo(resource.getParentResourceNo(), isIgnore);

        resource.setResourceNo(resourceNo);

        resourceMapper.insert(resource);

    }

    //根据父资源编号生成子资源编号
    private String createResourceNo(String parentResourceNo, Boolean isIgnore) {

        //判断菜单节点是否超过三级
        if(isIgnore&&(NoProcessing.processString(parentResourceNo).length()/3)>=5){
            //菜单节点超过三级
            throw new BaseException(BasicEnum.RESOURCE_DEPTH_UPPER_LIMIT);
        }

        ResourceDto dto = ResourceDto.builder().parentResourceNo(parentResourceNo).build();
        List<Resource> resourceList = resourceMapper.selectList(dto);
        if (EmptyUtil.isNullOrEmpty(resourceList)) {
            //没有子节点，创建新的资源编号
            return NoProcessing.createNo(parentResourceNo, false);
        }else {
            //存在子节点。在现有节点下进行追加
            Long maxresourceNo = resourceList.stream().map(resource -> {
                return Long.valueOf(resource.getResourceNo());
            }).max(Comparator.comparing(i -> i)).get();
            return NoProcessing.createNo(maxresourceNo.toString(), true);
        }
    }

    private void recursionTreeItem(List<TreeItemVo> treeItemVoList, Resource rootResource, List<Resource> resourceList) {
        TreeItemVo treeItem = TreeItemVo.builder()
                .id(rootResource.getResourceNo())
                .label(rootResource.getResourceName())
                .build();
        List<Resource> childrenResourceList = resourceList.stream()
                .filter(n -> n.getParentResourceNo().equals(rootResource.getResourceNo())).collect(Collectors.toList());

        if (!EmptyUtil.isNullOrEmpty(childrenResourceList)) {
            List<TreeItemVo> listChildren = new ArrayList();
            childrenResourceList.forEach(resource -> {
                this.recursionTreeItem(listChildren, resource, resourceList);
            });
            treeItem.setChildren(listChildren);
        }
        treeItemVoList.add(treeItem);
    }
}
